var GameOver = cc.Sprite.extend({
    _scoreSprite: null,
    _bestSprite: null,
    Sprite: null,
    ctor: function(){
        this._super();
    },
    init: function(obj){
        var bRet = false;
        if (this._super()) {
            var winSize = cc.Director.getInstance().getWinSize();
            
            var go = cc.Sprite.create(s_game_over);
            go.setAnchorPoint(cc.p(0.5, 0));
            go.setPosition(cc.p(winSize.width / 2, winSize.height));
            this.addChild(go, 0, 1);
            
            var bottom = cc.Sprite.create();
            var sp = cc.Sprite.create(s_score_panel);
            bottom.addChild(sp);
            //重新开始按钮
            var reStartItem = cc.MenuItemImage.create(s_button_play, s_button_play, this.onRestart, this);
            reStartItem.setAnchorPoint(cc.p(0.5, 0.5));
            
            var menu = cc.Menu.create(reStartItem);
            menu.setPosition(cc.PointZero());
            bottom.addChild(menu, 1);
            reStartItem.setPositionY(-118);
            
            
            bottom.setPosition(cc.p(winSize.width / 2, -winSize.height / 2));
            this.addChild(bottom);
            
            //medal
            var medal = cc.Sprite.create();
            this._medalSprite = medal;
            medal.setPosition(cc.p(-66, -3));
            bottom.addChild(medal);
            //score
            var score = cc.Sprite.create();
            this._scoreSprite = score;
            bottom.addChild(score, 10);
            //best
            var best = cc.Sprite.create();
            this._bestSprite = best;
            bottom.addChild(best, 10);
            
            go.runAction(cc.Sequence.create(cc.FadeOut.create(0), cc.DelayTime.create(0.25), cc.FadeIn.create(0.25)));
            sound.playSwooshingSound();
            go.runAction(cc.Sequence.create(cc.MoveBy.create(0.5, cc.p(0, -(120 + go.getContentSize().height))), cc.MoveBy.create(0.1, cc.p(0, 10)), cc.MoveBy.create(0.1, cc.p(0, -10)), cc.DelayTime.create(0.5), cc.CallFunc.create(function(){
                bottom.runAction(cc.MoveBy.create(0.5, cc.p(0, winSize.height)));
                sound.playSwooshingSound();
            })));
            
            this.setScore();
            this.setBest();
            this.setMedal();
            
            bRet = true;
        }
        return bRet;
    },
    setMedal: function(){
        var s = config.getScore(), n = 0;
        if (s >= 10 && s < 20) {
            n = 1;
        } else if (s >= 20 && s < 30) {
            n = 2;
        } else if (s >= 30 && s < 40) {
            n = 3;
        } else if (s >= 40) {
            n = 4;
        }
        if (n != 0) {
            this._medalSprite.addChild(cc.Sprite.create(s_medal(n)));
        }
    },
    setScore: function(){
        var winSize = cc.Director.getInstance().getWinSize();
        var s = config.getScore() + "", w = 0;
        this._scoreSprite.removeAllChildren(true);
        for (var i = 0; i < s.length; i++) {
            var num = cc.Sprite.create(s_s_font(s[i]));
            num.setPosition(i * 16, 0);
            w = i * 16;
            this._scoreSprite.addChild(num);
        }
        this._scoreSprite.setPosition(84 - w, 16);
    },
    setBest: function(){
        var winSize = cc.Director.getInstance().getWinSize();
        var s = Math.max(config.getBestScore(), config.getScore()) + "", w = 0;
        this._bestSprite.removeAllChildren(true);
        for (var i = 0; i < s.length; i++) {
            var num = cc.Sprite.create(s_s_font(s[i]));
            num.setPosition(i * 16, 0);
            w = i * 16;
            this._bestSprite.addChild(num);
        }
        config.setBestScore(parseInt(s));
        this._bestSprite.setPosition(84 - w, -22);
    },
    onRestart: function(){
        cc.log("--- Get Restart ---");
        sound.playSwooshingSound();
        cc.Director.getInstance().replaceScene(cc.TransitionFade.create(0.6, new ReStartScene()));
    }
});
GameOver.create = function(){
    var g = new GameOver();
    if (g && g.init()) {
        return g;
    }
    return null;
}
